define(["angularAMD"], function (angularAMD) {
    angularAMD.service('AbnormalService', ['$rootScope', '$filter', '$calculater', 'config', 'MMWService', '$mdDialog',
        function($rootScope, $filter, $calculater, config, $MMWService, $mdDialog){
            function genBadType(badReasonList){
                var badTypeList = [];
                var badType = '';
                badReasonList.forEach(function(badReason){
                    if(badType!=badReason.reason_type){
                        badTypeList.push({
                            reason_type:badReason.reason_type,
                            reason_sub_type:badReason.reason_sub_type
                        });
                        badType = badReason.reason_type;
                    }
                });
                return badTypeList;
            }

            function openDialog(options, badCodeList){
                console.log('options',options);
                var badTypeList = genBadType(badCodeList),
                    default_badType = {abnormal_type_name:'', abnormal_type:''};
                if(badTypeList.length == 1){
                    default_badType.abnormal_type_name = badTypeList[0].reason_sub_type;
                    default_badType.abnormal_type = badTypeList[0].reason_type;
                }
                //備份本來的例外原因
                var abnormal_detail_backup = options.plotInfo.abnormal_detail.slice();
                $mdDialog.dialog(
                    'module/moduleB/program/B0301/abnormal_detail.tmp.html',
                    function(dialog){
                        return {
                            css_url:'module/moduleB/program/B0301/abnormal_detail.css',
                            plotInfo : options.plotInfo,
                            abnormal_type_name:default_badType.abnormal_type_name,
                            abnormal_type:default_badType.abnormal_type,
                            abnormal_no : '',
                            abnormal_name : '',
                            defect_qty : 0,
                            //20180518 modify by Dustdusk for M#46295: 增加例外類別
                            loadBadTypeList : function(){
                                $rootScope.showSelect({
                                    label: 'reason_sub_type',
                                    code: 'reason_type',
                                    selectCode: dialog.abnormal_type,
                                    list: badTypeList,
                                    confirm: function (item, radioDialog) {
                                        radioDialog.hide();
                                        dialog.abnormal_type_name = item.reason_sub_type;
                                        dialog.abnormal_type = item.reason_type;
                                    }
                                });
                            },
                            //20180518 modify by Dustdusk for M#46295: 增加例外類別
                            loadBadReasonList : function(){
                                if(dialog.abnormal_type != ''){
                                    $rootScope.showQueryList({
                                        key:'reason_no',
                                        title:$filter('translate')('common.queryList.badReason_select'),
                                        header : [
                                            {label: $filter('translate')('common.queryList.reason_c'), code:'reason_no'},
                                            {label: $filter('translate')('common.queryList.reason_n'), code:'reason_name'}
                                        ],
                                        query: function(conditions, feedback){
                                            feedback($filter('filter')(badCodeList, {reason_type:dialog.abnormal_type}));
                                        },
                                        confirm: function(selectItem, radioDialog){
                                            radioDialog.hide();
                                            dialog.abnormal_no = selectItem.reason_no;
                                            dialog.abnormal_name = selectItem.reason_name;
                                        }
                                    });
                                } else {
                                    $rootScope.showAlert($filter('translate')('B0301.msg.bad_type_empty'));
                                }
                            },
                            add : function(){
                                var msg = '';
                                if(dialog.abnormal_type == ''){
                                    msg += $filter('translate')('B0301.msg.abnormal_type_empty')+'<br>';
                                }
                                if(dialog.abnormal_no == ''){
                                    msg += $filter('translate')('B0301.msg.abnormal_no_empty')+'<br>';
                                }
                                if(dialog.defect_qty == 0){
                                    msg += $filter('translate')('B0301.msg.defect_qty_zero')+'<br>';
                                }
                                //20200518 modify by Dustdusk for M#: 增加重複檢核
                                //重複檢核
                                for (var i = 0; i < dialog.plotInfo.abnormal_detail.length; i++){
                                    //原因重複
                                    if(dialog.plotInfo.abnormal_detail[i].abnormal_no == dialog.abnormal_no){
                                        msg += dialog.abnormal_no + $filter('translate')('B0301.msg.reason_duplicate');
                                        break;
                                    }
                                }
                                if(msg == ''){
                                    dialog.plotInfo.abnormal_detail.push({
                                        abnormal_type : dialog.abnormal_type,
                                        abnormal_type_name : dialog.abnormal_type_name,
                                        abnormal_no : dialog.abnormal_no,
                                        abnormal_name : dialog.abnormal_name,
                                        defect_qty : dialog.defect_qty
                                    });
                                    dialog.abnormal_no = '';
                                    dialog.abnormal_name = '';
                                    dialog.defect_qty = 0;
                                } else {
                                    $rootScope.showAlert(msg);
                                }
                            },
                            remove: function(index){
                                dialog.plotInfo.abnormal_detail.splice(index, 1);
                            },
                            confirm : function(){
                                if(options.confirm){
                                    options.confirm(dialog);
                                } else {
                                    dialog.hide();
                                }
                            },
                            back : function(){
                                if(options.close){
                                    options.close(dialog, abnormal_detail_backup.slice());
                                } else {
                                    options.plotInfo.abnormal_detail = abnormal_detail_backup.slice();
                                    dialog.hide();
                                }
                            }
                        }
                    });
            }
            /**
             * options:{
             *     plotInfo : {
             *         plot_no : '',
             *         op_no : ''
             *     }
             *     limit_abnormal_type : []//限制只能使用那些類別
             * }
             */
            this.init = function(options){
                //取得所有例外原因
                $MMWService.sendToServer({
                    uri : 'ServicesSTD.Module_WIP.qc_reason_basis_info_get',
                    content : {
                        op_no : options.plotInfo.op_no
                    },
                    success : function(data){
                        var badreason_list;
                        if(options.limit_abnormal_type && options.limit_abnormal_type.length>0)
                            badreason_list = $filter('filter')(data.qc_reason_basis, {reason_type:options.limit_abnormal_type[0]});
                        else
                            badreason_list = data.qc_reason_basis;
                        if(badreason_list && badreason_list.length>0){
                            openDialog(options, badreason_list);
                        } else {
                            $rootScope.showAlert($filter('translate')('common.abnormal_detail.msg.abnormal_detail_empty'));
                        }
                    }
                });
            };
        }]);
});
